package com.yfve.eol.util;

import android.provider.Settings;
import android.util.Log;

import com.yfve.eol.EolApplication;

public class BackLightUtil {
    public static final int NORMAL_BRIGHTNESS = 255;
    private static final String TAG = "BackLightUtil";
    private static final int MIN_BRIGHTNESS = 5;
    private static final int MAX_BRIGHTNESS = 1023;
    private static final int BRIGHTNESS_LEVEL = 10;

    public static void stopAutoBrightness() {
        Log.i(TAG, "stopAutoBrightness: ");
        Settings.System.putInt(EolApplication.getAppContext().getContentResolver(),
                Settings.System.SCREEN_BRIGHTNESS_MODE, Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
    }

    public static void handleSetScreenIllumination(int id, byte[] valueBytes) {
        Log.i(TAG, "processSetScreenIllumination: ID_EOLTESTSCREENILL_SET + byte length = " + valueBytes.length);
        if (valueBytes.length < 1) {
            Log.i(TAG, "processSetScreenIllumination: wrong value length");
            return;
        }
        Log.i(TAG, "processSetScreenIllumination: value0 = " + valueBytes[0]);
        if (isAutoBrightness()) {
            stopAutoBrightness();
            Log.i(TAG, "close auto bright");
        }
        int brightnessValue = MAX_BRIGHTNESS;
        int value = valueBytes[0];
        if ((value >= 0) && (value < BRIGHTNESS_LEVEL)) {
            brightnessValue = MAX_BRIGHTNESS * value / BRIGHTNESS_LEVEL;
        }

        if (brightnessValue == 0) {
            Log.i(TAG, "handleSetScreenIllumination: value = 0, close back lisht");
            Rl78Util.getInstance().setRl78PowerEnable(false);
            Util.setByteProperty(id, true);
            return;
        }

        if (!Rl78Util.getInstance().isRl78PowerEnable()) {
            Log.i(TAG, "handleSetScreenIllumination: back light has been disabled");
            Rl78Util.getInstance().setRl78PowerEnable(true);
        }

        Log.i(TAG, "brightnessValue = " + brightnessValue);

        brightnessValue = Util.middleValue(brightnessValue, MIN_BRIGHTNESS, MAX_BRIGHTNESS);

        try {
            Settings.System.putInt(EolApplication.getAppContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightnessValue);
            Util.setByteProperty(id, true);
        } catch (Exception e) {
            e.printStackTrace();
            Util.setByteProperty(id, false);
        }
    }

    public static boolean isAutoBrightness() {
        boolean automicBrightness = false;
        try {
            automicBrightness = Settings.System.getInt(EolApplication.getAppContext().getContentResolver(),
                    Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;
        } catch (Settings.SettingNotFoundException e) {
            e.printStackTrace();
        }
        Log.i(TAG, "isAutoBrightness: " + automicBrightness);
        return automicBrightness;
    }

    public static int getCurrentBrightness() {
        return Settings.System.getInt(EolApplication.getAppContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, NORMAL_BRIGHTNESS);
    }

    public static boolean setBrightness(int brightness) {
        Log.i(TAG, "setBrightness: set brightness = " + brightness);
        if (isAutoBrightness()) {
            stopAutoBrightness();
            Log.i(TAG, "close auto bright");
        }

        if (brightness == 0) {
            Rl78Util.getInstance().setRl78PowerEnable(false);
            return true;
        } else {
            if (!Rl78Util.getInstance().isRl78PowerEnable()) {
                Rl78Util.getInstance().setRl78PowerEnable(true);
            }
        }

        brightness = Util.middleValue(brightness, MIN_BRIGHTNESS, MAX_BRIGHTNESS);

        try {
            Settings.System.putInt(EolApplication.getAppContext().getContentResolver(), Settings.System.SCREEN_BRIGHTNESS, brightness);
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }
}
